package lium.lbs.lbsim.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.abbott.mutiimgloader.util.JImageLoader
import com.abbott.mutiimgloader.weixin.WeixinMerge
import com.buz.hjcdriver.bean.ApplyAddQunBean
import com.lmlibrary.Constants
import com.lmlibrary.bean.ResponseBean
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_group_infor.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.bean.GroupInfoBean
import lium.lbs.lbsim.jgchat.application.GJApp
import lium.lbs.lbsim.utils.SpUtils
import lium.lbs.lbsim.utils.ToastUtils
import lium.lbs.lbsim.utils.callbck.DialogCallback
import java.util.HashMap

class GroupInforActivity : BaseActivity() {

    var groupId: String? = null
    var myGroupId: String? = null
    var groupInfoBean: GroupInfoBean? = null
    var url: List<String>? = null
    var imageLoader: JImageLoader? = null
    var groupName: String? = null

    var togroupId: String? = null
    var tomyGroupId: String? = null
    var displayName: String? = null
    var price: String? = null



    override fun getLayoutId(): Int {
        return R.layout.activity_group_infor
    }


    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("群详情")
        groupId = intent.getStringExtra(GJApp.GROUP_ID)
        myGroupId = intent.getStringExtra(GJApp.MY_GROUP_ID)
        imageLoader = JImageLoader(this)
        addOnClickListeners(
            R.id.contacts_add,
            R.id.contacts_jion
        )
    }


    override fun getNetWorkData() {
        getDataList()
    }

    fun getDataList() {
        val param = HashMap<String, String>()
        param["memberId"] = SpUtils.getInstance().get(Constants.memberId, "") as String
        param["gid"] = groupId.toString()
        postData(
            "/group/groupInfo",
            param,
            object : DialogCallback<ResponseBean<GroupInfoBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<GroupInfoBean>>) {
                    setDataList(response.body().data)
                }
            })
    }


    private fun setDataList(groupInfoBean: GroupInfoBean) {
        this.groupInfoBean = groupInfoBean
        contacts_name.text = groupInfoBean.group.groupName
        contacts_dec.text = if (TextUtils.isEmpty(groupInfoBean.group.notice)){"暂无群公告!"} else {groupInfoBean.group.notice}
        contacts_number.text = groupInfoBean.group.memberNum + "个人"
        contacts_gid.text = groupId.toString()
        price = groupInfoBean.group.price
        groupName = groupInfoBean.group.groupName
        togroupId = groupInfoBean.group.gid
        tomyGroupId = groupInfoBean.group.groupId
        if(groupInfoBean.group.headImage.size > 0){
            imageLoader!!.displayImages(groupInfoBean.group.headImage, contacts_head, WeixinMerge())
        }


        if (groupInfoBean.group.isJoin.equals("1")) {//isJoin   0 没加入  1.已加入
            contacts_jion.visibility = View.VISIBLE
            contacts_add.visibility = View.GONE
        } else {
            contacts_jion.visibility = View.GONE
            contacts_add.visibility = View.VISIBLE
        }

    }

    override fun onClick(v: View?) {
        val intent = Intent()
        super.onClick(v)
        when (v!!.id) {

            R.id.contacts_add -> {
                getApplyAddQun()
            }
            R.id.contacts_jion -> {
                intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                //设置跳转标志
                intent.putExtra("fromGroup", false)
                intent.putExtra(GJApp.MEMBERS_COUNT, 0 + 1)
                intent.putExtra(GJApp.GROUP_ID, togroupId)
                intent.putExtra(GJApp.MY_GROUP_ID, tomyGroupId)
                intent.putExtra(GJApp.CONV_TITLE, groupName)
                intent.setClass(this, lium.lbs.lbsim.jgchat.activity.ChatActivity::class.java)
                startActivity(intent)
            }

        }


    }

    fun getApplyAddQun() {
        val param = HashMap<String, String>()
        param["memberId"] = SpUtils.getInstance().get(Constants.memberId, "") as String
        param["groupId"] = myGroupId.toString()
        postData(
            "/groupmember/memberApplyGroup",
            param,
            object : DialogCallback<ResponseBean<ApplyAddQunBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<ApplyAddQunBean>>) {
                    setApplyAddQun(response.body().data)
                }
            })
    }

    private fun setApplyAddQun(inviteApplyMemberBean: ApplyAddQunBean) {

        when (inviteApplyMemberBean.type) {//1,付费  2，身份验证 3，回答问题  4直接入群


            "1" -> {//付费
                intent.setClass(this, InGroupByMoneyActivity::class.java)
                intent.putExtra(GJApp.MY_GROUP_ID, inviteApplyMemberBean.groupId)
                intent.putExtra("applyId", inviteApplyMemberBean.applyId)
                intent.putExtra("orderSn", inviteApplyMemberBean.orderSn)
                intent.putExtra("price", price)
                intent.putExtra("groupName", groupName)
                startActivityForResult(intent, 100)
            }
            "1" -> {//2，身份验证

                ToastUtils.showToast("需要实名认证验证身份")
            }
            "3" -> {//回答问题
                intent.setClass(this, InGroupByQuestionActivity::class.java)
                intent.putExtra(GJApp.MY_GROUP_ID, inviteApplyMemberBean.groupId)
                intent.putExtra("applyId", inviteApplyMemberBean.applyId)
                intent.putExtra("question", inviteApplyMemberBean.question)
                intent.putExtra("groupName", groupName)
                startActivityForResult(intent, 100)
            }

            "4" -> {//4直接入群
                intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                //设置跳转标志
                intent.putExtra("fromGroup", false)
                intent.putExtra(GJApp.MEMBERS_COUNT, 0 + 1)
                intent.putExtra(GJApp.GROUP_ID, togroupId)
                intent.putExtra(GJApp.MY_GROUP_ID, tomyGroupId)
                intent.putExtra(GJApp.CONV_TITLE, groupName)
                intent.setClass(this, lium.lbs.lbsim.jgchat.activity.ChatActivity::class.java)
                startActivity(intent)
            }
        }
    }
}
